﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;

namespace DataManager.DAL
{
    public struct Actor
    {
        int m_ActorId;
        string m_ActorName;
        string m_Nationality;

        public int ActorId
        {
            get { return m_ActorId; }
            set { m_ActorId = value; }
        }

        public string ActorName
        {
            get { return m_ActorName; }
            set { m_ActorName = value; }
        }

        public string Nationality
        {
            get { return m_Nationality; }
            set { m_Nationality = value; }
        }
    }

    class Actors:DAL
    {
        /// <summary>
        /// Visszateriti a szineszek listajat a kereses alapjan, ha nincs parameter akkor az osszeset
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<Actor> GetActorList(string name)
        {
            string query = "SELECT SzineszId, SzineszNev, Nemzetiseg "+
                " FROM Szineszek ";
            if (name != "")
            {
                query = query + "WHERE SzineszNev LIKE '%" + name + "%'";
            }
            string error = string.Empty;
            SqlDataReader rdr = ExecuteReader(query, ref error);
            List<Actor> actorList = new List<Actor>();
            if (error == "OK")
            {
                while (rdr.Read())
                {
                    Actor item = new Actor();
                    item.ActorId = Convert.ToInt32(rdr["SzineszId"]);
                    item.ActorName = rdr["SzineszNev"].ToString();
                    item.Nationality = rdr["Nemzetiseg"].ToString();
                    actorList.Add(item);
                }
            }
            CloseDataReader(rdr);
            return actorList;
        }

        /// <summary>
        /// Tarolt eljaras hasznalataval beszur egy uj szineszt.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="nationality"></param>
        /// <returns></returns>
        public string InsertActor(string name, string nationality)
        {
            string[] parameterNames = new string[2], parameterValues = new string[2];
            parameterNames[0] = "@Nev"; parameterValues[0] = name.ToString();
            parameterNames[1] = "@Nemzetiseg"; parameterValues[1] = nationality.ToString();

            return ExecuteStoredProcedureNonQuery("UjSzinesz", parameterNames, parameterValues);
        }

        /// <summary>
        /// A megfelelo Id-ju szinesz adatait modositja
        /// </summary>
        /// <param name="actorId"></param>
        /// <param name="name"></param>
        /// <param name="nationality"></param>
        /// <returns></returns>
        public string UpdateActor(int actorId, string name, string nationality)
        {
            string query = "UPDATE Szineszek " +
                "SET SzineszNev = '" + name + "', " +
                "Nemzetiseg = '" + nationality + "' " +
                "WHERE SzineszId = " + actorId;
            return ExecuteNonQuery(query);
        }

        /// <summary>
        /// A megadott Idju szineszt torli.
        /// </summary>
        /// <param name="actorId"></param>
        /// <returns></returns>
        public string DeleteActor(int actorId)
        {
            string query = "DELETE FROM Szineszek WHERE SzineszId = " + actorId;
            return ExecuteNonQuery(query);
        }
    }
}

        
